package com.ln.spring.core;

import lombok.Data;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.PropertyValue;

import java.util.Arrays;

/**
 * https://my.oschina.net/thinwonton/blog/1492224#h1_3
 * https://juejin.cn/post/6844904047070167054
 */
public class BeanWrapperDemo {

    public static void main(String[] args) {

        //左边轮子的BeanWrapper
        Wheel leftWheel = new Wheel();
        BeanWrapper beanWrapperOfLeftWheel = PropertyAccessorFactory.forBeanPropertyAccess(leftWheel);
        PropertyValue leftPosition = new PropertyValue("position", "左边");
        beanWrapperOfLeftWheel.setPropertyValue(leftPosition);
        System.out.println(beanWrapperOfLeftWheel.getWrappedInstance());

        //左边轮子的BeanWrapper
        Wheel rightWheel = new Wheel();
        BeanWrapper beanWrapperOfRightWheel = PropertyAccessorFactory.forBeanPropertyAccess(rightWheel);
        PropertyValue rightPosition = new PropertyValue("position", "右边");
        beanWrapperOfRightWheel.setPropertyValue(rightPosition);
        System.out.println(beanWrapperOfRightWheel.getWrappedInstance());

        // 驾驶员
        Driver driver = new Driver();
        BeanWrapper beanWrapperOfDriver = PropertyAccessorFactory.forBeanPropertyAccess(driver);
        PropertyValue age = new PropertyValue("age", 20);
        beanWrapperOfDriver.setPropertyValue(age);
        System.out.println(beanWrapperOfDriver.getWrappedInstance());


        // 车子
        Car car = new Car();
        BeanWrapper beanWrapperOfCar = PropertyAccessorFactory.forBeanPropertyAccess(car);
        beanWrapperOfCar.setPropertyValue("name", "奔驰"); // 车名
        Wheel[] wheels = {leftWheel, rightWheel}; //轮子数组
        beanWrapperOfCar.setPropertyValue("wheels", wheels); //轮子
        beanWrapperOfCar.setPropertyValue("driver", driver); //驾驶员
        System.out.println(beanWrapperOfCar.getWrappedInstance());

        // 获取驾驶员的年龄
        int retrievedAge = (Integer) beanWrapperOfCar.getPropertyValue("driver.age");
        System.out.println("driver age : " + retrievedAge);


        // 通过表达式间接设置car的wheel的width
        beanWrapperOfCar.setPropertyValue("wheels[0].position", "修改过的左边");
        System.out.println(beanWrapperOfCar.getWrappedInstance());

    }
    

    @Data
    public static class Car {
        private String name;
        private Wheel[] wheels;
        private Driver driver;
        @Override
        public String toString() {
            return "Car{" +
                    "name='" + name + '\'' +
                    ", wheels=" + Arrays.toString(wheels) +
                    ", driver=" + driver +
                    '}';
        }
    }

    @Data
    public static class Driver {
        private int age;
        @Override
        public String toString() {
            return "Driver{" +
                    "age=" + age +
                    '}';
        }
    }

    @Data
    public static class Wheel {
        private String position;

        @Override
        public String toString() {
            return "Wheel{" +
                    "position='" + position + '\'' +
                    '}';
        }
    }
    
}
